<?php
/**
 * 商品控制器
 */
namespace app\index\controller;
use app\index\controller\Common;
class Comment extends Common{

    /**
     * 异步添加评论
     */
    public function addComment(){

        //安全处理 
        if( !request()->isAjax() ){

            $this->error('您访问的页面不存在！');

        }

        //接收信息
        $gid = input('post.id')+0;
        $data['stars'] = input('post.stars')+0;
        $data['content'] = input('post.comment');
        $data['addtime'] = time();
        $data['mid'] = session('mid');
        $data['gid'] = $gid;
        

        //验证信息
        if( !preg_match('/[12345]/', $data['stars']) ) {

            echo json_encode(['status'=>false,'msg'=>'星数必须是1~5之间的数字!']);
            die;
        }

        if( $data['content'] === '' ) {

            echo json_encode(['status'=>false,'msg'=>'请填写评论内容!']);
            die;
        }

        //添加评论
        $cModel = db('comment');
        if( $cModel->strict(true)->insert($data) ) {

            $id = $cModel->getLastInsID();

            //如果填写了印象
            if( input('post.impression') ) {

                //实例化模型
                $model = db('impression');

                //将印象拆分成数组
                $title = explode(',', input('post.impression'));

                //挨个插入
                foreach ($title as $v) {
                
                    $info['title'] = $v;
                    $info['gid'] = $gid;

                    if( $model->where(['gid'=>$gid,'title'=>$v])->value('id') ) {

                        $model->where(['gid'=>$gid,'title'=>$v])->setInc('num');

                    }else {

                        $model->strict(true)->insert($info);
                    }

                    
                }

            }
                
            echo json_encode(['status'=>true,'msg'=>'添加成功!','id'=>$id,'time'=>date('Y-m-d H:i:s',$data['addtime'])]);

        }else {
            echo json_encode(['status'=>false,'msg'=>'添加失败!']);
        }

    }

    /**
     * 异步获取五条评论
     */
    public function getComment(){

        //安全处理 
        if( !request()->isAjax() ){

            $this->error('您访问的页面不存在！');

        }

        //接收信息
        $id = input('post.id')+0;
        $startIndex = input('post.startIndex')+0;
        $getCommentNum = input('post.getCommentNum')+0;

        //添加评论
        $comments = db('comment as c')->field('c.*,m.nickname,m.email')->join('jd_member m','c.mid = m.mid','LEFT')->where('gid','=',$id)->order('cid desc')->limit($startIndex,$getCommentNum)->select();

        if( array_filter( $comments ) ) {

            foreach ($comments as $k => $v) {
                $comments[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }

            echo json_encode(['status'=>true,'msg'=>$comments]);

        }else {
            echo json_encode(['status'=>false,'msg'=>'获取失败或没有更多评论!']);
        }

    }

    /**
     * 异步添加评论有用
     */
    public function addCommentUsed(){

        //安全处理 
        if( !request()->isAjax() ){

            $this->error('您访问的页面不存在！');

        }

        //接收信息
        $data['cid'] = input('id')+0;
        $data['mid'] = session('mid');

        if( !$data['mid'] ){

            echo json_encode(['status'=>false,'msg'=>'登录后才可以使用此功能！']);
            die;
        }
        

        //验证用户是否已经添加了有用
        $model = db('comment_use');
        if( $model->where($data)->value('mid') ) {

            echo json_encode(['status'=>false,'msg'=>'你已经发表过是否有用的观点！']);
            die;
        }

        //添加评论
        if( $model->strict(true)->insert($data) ) {

            echo json_encode(['status'=>true,'msg'=>'添加成功!']);

        }else {
            echo json_encode(['status'=>false,'msg'=>'添加失败!']);
        }

    }

    /**
     * 异步添加评论有用
     */
    public function reply(){

        // 检测是否登录
        if( !session('mid') ){

            $this->error('登录后才能进行回复！');

        }

        // 设置页面信息
        $this->setPageMsg('评论回复页', '回复页关键字', '回复页描述', ['service','ui-page','common','bottomnav','jqzoom'], ['goods','jqzoom-core'] );

        //获取信息
        $id = input('cid');

        //获取评论
        $comment = db('comment as c')->field('c.*,m.nickname,m.email')->join('jd_member m','c.mid = m.mid','LEFT')->where('cid','=',$id)->find( $id );

        // 获取回复
        $replys = db('reply as r')->field('r.*,m.nickname,m.email')->join('jd_member m','r.mid = m.mid','LEFT')->where('cid','=',$id)->order('rid desc')->paginate( config('paginate.list_rows') );

        //分配变量
        $this->assign('comment',$comment);
        $this->assign('replys',$replys);

        // 展示视图
        return $this->fetch();
    }

    /**
     * 异步添加回复
     */
    public function addReply(){

        //安全处理 
        if( !request()->isAjax() ){

            $this->error('您访问的页面不存在！');

        }

        //接收信息
        $data['cid'] = input('post.id')+0;
        $data['mid'] = session('mid');
        $data['addtime'] = time();
        $data['content'] = input('post.content');
        

        //验证信息

        if( $data['content'] === '' ) {

            echo json_encode(['status'=>false,'msg'=>'请填写回复内容!']);
            die;
        }

        //添加评论
        if( db('reply')->strict(true)->insert($data) ) {
                
            echo json_encode(['status'=>true,'msg'=>'添加成功!','time'=>date('Y-m-d H:i:s',$data['addtime'])]);

        }else {
            echo json_encode(['status'=>false,'msg'=>'添加失败!']);
        }

    }

    /**
     * 异步获取印象
     */
    public function getImpression(){

        //安全处理 
        if( !request()->isAjax() ){

            $this->error('您访问的页面不存在！');

        }

        //接收信息
        $id = input('id')+0;

        
        //获取商品印象
        $impressions = db('impression')->field('title,num')->where('gid','=',$id)->order('id desc')->limit(8)->select();
       
        if( $impressions ) {

            echo json_encode(['status'=>true,'msg'=>$impressions]);

        }else {
            echo json_encode(['status'=>false,'msg'=>'获取失败或还没人添加印象!']);
        }

    }

    /**
     * 异步获取好评度
     */
    public function getCommentRate(){

        //安全处理 
        if( !request()->isAjax() ){

            $this->error('您访问的页面不存在！');

        }

        //接收信息
        $id = input('id')+0;

        
        //获取商品评论
        $data = db('comment')->field('stars')->where('gid','=',$id)->select();

        // 计算好评度（4、5星为好评,2、3星为中评，1星为差评）
        $goodNums = 0;
        $mediumNums = 0;
        $total = count($data) ? count($data) : 0;
        if( $data ){

            foreach ($data as $v) {
                
                if( $v["stars"] > 3 ){

                    $goodNums++;

                }else if( $v["stars"] > 1 ){

                    $mediumNums++;

                }

            }
        }
        $commentRate["good"] = $total == 0 ? 0 : ceil( $goodNums/$total*100 );
        $commentRate["medium"] = $total == 0 ? 0 : ceil( $mediumNums/$total*100);
        $commentRate["bad"] = $total == 0 ? 0 : 100-$commentRate["good"]-$commentRate["medium"];

        if( $commentRate ) {

            echo json_encode(['status'=>true,'msg'=>$commentRate,'total'=>count($data)]);

        }else {
            echo json_encode(['status'=>false,'msg'=>'获取失败或还没人添加印象!']);
        }

    }





}
